import Random
import Base
using PyCall

@pyimport pygame

IMG_SIZE = 80

function fillBoard!(m, x, y, board)
    for r in 1:3
        for c in 1:3
            board[y + r][x + c] = m[r, c]
        end
    end
end

function renderBoard(surface, board, images)
    for r in 1:9
        for c in 1:9
            x = (c - 1) * IMG_SIZE + IMG_SIZE ÷ 2
            y = (r - 1) * IMG_SIZE + IMG_SIZE ÷ 2
            if board[r][c] == 0
                surface.blit(images["empty"], (x, y))    
            else
                num = board[r][c]
                surface.blit(images[num], (x, y))
            end
        end
    end
end

function main()
    pygame.init()
    pygame.display.set_caption("sudoku")
    screen = pygame.display.set_mode((800, 800))
    screen.fill((255,255,255))
    images = Dict()
    count = rand(30:50)
    for i in 1:9
        images[i] = pygame.image.load(string("./images/", string(i), ".png"))
    end

    images["empty"] = pygame.image.load(string("./images/", "white.png"))

    board = [[0 for i in 1:9] for i in 1:9]

    mid = Random.shuffle([i for i in 1:9])

    m = [mid[1:3] mid[4:6] mid[7:9]]
    
    fillBoard!(m, 3, 3, board)

    mr1 = m[1, :] 
    mr2 = m[2, :]
    mr3 = m[3, :]

    mc1 = m[:, 1] 
    mc2 = m[:, 2]
    mc3 = m[:, 3]

    ml = [mr2 mr3 mr1]'
    fillBoard!(ml, 0, 3, board)
    mr = [mr3 mr1 mr2]'
    fillBoard!(mr, 6, 3, board)

    mu = [mc2 mc3 mc1]
    fillBoard!(mu, 3, 0, board)
    md = [mc3 mc1 mc2]
    fillBoard!(md, 3, 6, board)

    mlc1 = ml[:, 1]
    mlc2 = ml[:, 2]
    mlc3 = ml[:, 3]

    mlt = [mlc2 mlc3 mlc1]
    fillBoard!(mlt, 0, 0, board)
    mlb = [mlc3 mlc1 mlc2]
    fillBoard!(mlb, 0, 6, board)

    mrc1 = mr[:, 1]
    mrc2 = mr[:, 2]
    mrc3 = mr[:, 3]

    mrt = [mrc2 mrc3 mrc1]
    fillBoard!(mrt, 6, 0, board)
    mrb = [mrc3 mrc1 mrc2]
    fillBoard!(mrb, 6, 6, board)

    while count > 0
        r = rand(1:9)
        c = rand(1:9)
        if board[r][c] == 0
            continue
        end
        board[r][c] = 0
        count -= 1
    end

    while true
        renderBoard(screen, board, images)
        for event in pygame.event.get()
            if event.type == pygame.QUIT
                pygame.quit()
            end
        end
        pygame.display.update()
    end

end

main()

